package com.xl.web.controller.sys;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xl.base.annotation.Auth;
import com.xl.base.annotation.Duplicate;
import com.xl.base.web.BaseController;
import com.xl.core.util.StringUtil;
import com.xl.web.entity.sys.SysMenuBtn;
import com.xl.web.service.sys.SysMenuBtnService;

/**
 * 系统后台菜单按钮管理控制器
 * 
 * @author 梁华
 * 
 */
@Controller
@Scope("prototype")
@RequestMapping("/sys/menuBtn")
public class SysMenuBtnController extends BaseController {

	// 注入系统菜单按钮管理service
	@Autowired(required = false)
	private SysMenuBtnService<SysMenuBtn> sysMenuBtnService;

	/**
	 * 菜单按钮列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menuBtn.list")
	@RequestMapping("/list")
	public ModelAndView list(SysMenuBtn menuBtn) throws Exception {

		Map<String, Object> context = getRootMap(request);
		context.put("entity", menuBtn);
		return forword("sys/menu_btn_list", context);
	}
	
	/**
	 * 菜单按钮列表数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.list")
	@ResponseBody
	@RequestMapping("/listData")
	public void listData() throws Exception {

		SysMenuBtn menuBtn = (SysMenuBtn)getJsonParams(SysMenuBtn.class);
		menuBtn.getPager().setOrderField("create_date");
		// 查询按钮列表
		List<SysMenuBtn> dataList = sysMenuBtnService.queryByList(menuBtn);
		sendListData(response, menuBtn.getPager().getRowCount(), dataList);
	}

	/**
	 * 新增菜单按钮页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menuBtn.handle")
	@Duplicate(add = true)
	@RequestMapping("/add")
	public ModelAndView add(SysMenuBtn menuBtn) throws Exception {

		Map<String, Object> context = getRootMap(request);
		context.put("menuBtn", menuBtn);

		return forword("sys/menu_btn_add", context);
	}

	/**
	 * 修改菜单按钮页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menuBtn.handle")
	@Duplicate(add = true)
	@RequestMapping("/update")
	public ModelAndView update(SysMenuBtn menuBtn) throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 查询本菜单按钮的详细数据
		menuBtn = sysMenuBtnService.queryById(menuBtn);
		context.put("menuBtn", menuBtn);

		return forword("sys/menu_btn_update", context);
	}

	/**
	 * 新增菜单按钮保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menuBtn.handle")
	@Duplicate(validate = true)
	@ResponseBody
	@RequestMapping("addSave")
	public void addSave(SysMenuBtn menuBtn) throws Exception {

		// 新增菜单按钮保存
		if (StringUtil.isEmpty(menuBtn.getId())) {
			sysMenuBtnService.add(menuBtn);
		} else {
			// 更新菜单按钮保存
			sysMenuBtnService.updateBySelective(menuBtn);
		}

		sendSuccessMessage(response, "添加成功");
	}

	/**
	 * 删除菜单按钮保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menuBtn.handle")
	@ResponseBody
	@RequestMapping("deleteMenuBtn")
	public void deleteMenu(SysMenuBtn menuBtn) throws Exception {

		// 逻辑删除本条数据
		sysMenuBtnService.delete(menuBtn.getId());

		sendSuccessMessage(response, "删除成功");
	}
}
